package com.woniuxy.mesboot.controller;

import com.woniuxy.mesboot.dto.MatStorageDTO;
import com.woniuxy.mesboot.entity.ChildrenNameId;
import com.woniuxy.mesboot.entity.MatStorage;
import com.woniuxy.mesboot.entity.Storage;
import com.woniuxy.mesboot.mapper.NameIdMapper;
import com.woniuxy.mesboot.service.MatStorageService;
import com.woniuxy.mesboot.service.MatinService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.vo.MatVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ms")
public class MatStorageController {
    private static Map<String, Object> map = new HashMap<>();
    @Resource
    private NameIdMapper nameIdMapper;
    @Resource
    private MatStorageService  matStorageService;
    @Resource
    private MatinService matinService;

    @GetMapping("queryInfo")
    public Result getNameIdOfMat() {
        List<ChildrenNameId> nameIdList = nameIdMapper.getNameIdOfMat();
        map.clear();
        map.put("options", nameIdList);
        return Result.ok().data(map);
    }

    @PutMapping("updateStorage")
    public Result updateStorage(@RequestBody MatStorageDTO storage) {
        int i = matStorageService.updateStorage(storage);
        if (i > 0) {
            return Result.ok("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    @GetMapping
    public Result  queryAll(){
        List<MatVO> matStorages=matStorageService.selectAll();
        return Result.ok().data("list",matStorages);
    }
    @PostMapping()
    public Result insert(@RequestBody MatStorage matStorage) {
        int i = matStorageService.insertSelective(matStorage);
        if (i > 0) {
            return Result.ok("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @GetMapping("{id}")
    public Result queryById(@PathVariable Long id) {
        MatStorage matStorage = matStorageService.selectByPrimaryKey(id);
        return Result.ok().data("mat", matStorage);
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id) {
        int i = matStorageService.deleteByPrimaryKey(id);
        if (i > 0) {
            return Result.ok("删除成功");

        } else {
            return Result.error("删除失败");
        }
    }

    @PutMapping
    public Result update(@RequestBody MatStorage orders) {
        int i = matStorageService.updateByPrimaryKeySelective(orders);
        if (i > 0) {
            return Result.ok("修改成功");

        } else {
            return Result.error("修改失败");
        }
    }
//添加物料
    @PutMapping("add")
    public Result addMat(@RequestBody MatStorageDTO record) {
        int i = matStorageService.addMat(record);
        if (i > 0) {
//生成入库清单表
            matinService.insertOrder(record);
            return Result.ok("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }
}
